﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace DetectScars.common
{
    class SerializationUtils
    {
        //序列化字典到运行路径的DefaultValue.txt中
        public static void SerializeDictionary(Dictionary<string, string> dict)
        {
            //path为程序路径+DefaultValue.txt
            string path = Path.Combine(Directory.GetCurrentDirectory(), "DefaultValue.txt");
            using (System.IO.StreamWriter file = new System.IO.StreamWriter(path))
            {
                foreach (var entry in dict)
                {
                    file.WriteLine(entry.Key + " " + entry.Value);
                }
            }
        }

        //反序列化DefaultValue.txt到字典
        public static Dictionary<string, string> DeserializeDictionary()
        {
            string path = Path.Combine(Directory.GetCurrentDirectory(), "DefaultValue.txt");
            Dictionary<string, string> dict = new Dictionary<string, string>();
            //文件不在则创建
            if (!File.Exists(path))
            {
                File.Create(path).Close();
            }
            using (System.IO.StreamReader file = new System.IO.StreamReader(path))
            {
                string? line;
                while ((line = file.ReadLine()) != null)
                {
                    string[] arr = line.Split(' ');
                    dict[arr[0]] = arr[1];
                }
            }
            return dict;
        }
    }
}
